/**
 * 
 */
package com.qachecklist.domain;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

/**
 * Environment in which the test was done.
 * 
 * Examples: Operating System, Browser
 * and/or hardware specifications
 * 
 * @author hirokiterashima
 */
@Entity
@Table(name="checklists")
public class CheckList extends PersistableDomain {

	protected static String fromQuery = "from CheckList";

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id = null;
	
	@Column(name="name")
    private String name;  // short-hand name to refer to this environment
	
	@Column(name="description")
	private String description;  // description of this environment

	@LazyCollection(LazyCollectionOption.FALSE)
	@ManyToMany( 
			targetEntity=Item.class, 
			cascade={CascadeType.ALL} 
			) 
			@JoinTable( 
			name="checklist_to_item",
			joinColumns = @JoinColumn(name="checklist_id"),
			inverseJoinColumns = @JoinColumn(name="item_id")
			) 
	private List<Item> items;
	
    public Long getId() {
        return id;
    }

    @SuppressWarnings("unused")
	private void setId(Long id) {
        this.id = id;
        
    }

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<Item> getItems() {
		return items;
	}

	public void setItems(List<Item> items) {
		this.items = items;
	}

	/**
	 * Returns an XML String of all the items in this checklist
	 * @return
	 */
	private String getItemsXMLString() {
		String xmlString = "";
		for (Item item : this.items) {
			xmlString += item.toXMLString();
		}
		return xmlString;
	}

	/**
	 * @see com.qachecklist.domain.PersistableDomain#getObjectClass()
	 */
	@Override
	protected Class<CheckList> getObjectClass() {
		return CheckList.class;
	}
	
	@Override
	public String toXMLString() {
		return "<checklist><id>"+ this.getId() +"</id>"
		+ "<name>"+this.getName()+"</name>"
		+ "<description>"+this.getDescription()+"</description>"
		+ getItemsXMLString() + "</checklist>";
	}
}
